﻿using System;
using System.Threading;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using jSLCore.Client.Presentation.jBinding;
using jSLCore.Utility.Extensions;

namespace jSLApp.Client.Controls.SoundPlayer
{
    public partial class EasyPlayer : UserControl
    {
        public EasyPlayer()
        {
            InitializeComponent();
            if (!System.ComponentModel.DesignerProperties.IsInDesignTool)
            {
                _timer = new Timer(player => UpdatePosition((EasyPlayer)player), this, new TimeSpan(0, 0, 1),
                                   new TimeSpan(0, 0, 1));
            }
        }

        private Timer _timer;
        private static void UpdatePosition(EasyPlayer player)
        {

          
            player.Dispatcher.BeginInvoke(() =>
                                              {

                                                  if (player.State == EasyPlayerState.Playing)
                                                  {
                                                      if (player.mainPlayer.CurrentState == MediaElementState.Playing)
                                                      {
                                                          
                                                              double k = player.mainPlayer.Position.TotalSeconds/
                                                                         player.mainPlayer.NaturalDuration.TimeSpan.
                                                                             TotalSeconds;

                                                          player.curPosition.Value = k;
                                                          player.UpdateText();
                                                      }
                                                  }
                                              });
            
        }

        private void UpdateText()
        {
            curPosition.Text = string.Format("{0} / {1}", mainPlayer.Position.ToHHMMSS(),mainPlayer.NaturalDuration.TimeSpan.ToHHMMSS());
        }

        private EasyPlayerState _state;
        private EasyPlayerState State
        {
            get { return _state; }
            set
            {
                switch (value)
                {
                    case EasyPlayerState.NoMedia:
                        playStop.Source =
                            new BitmapImage(new Uri("../../Resources/player/play_Disabled.png", UriKind.RelativeOrAbsolute));
                      
                        UpdateText();
                        
                        curPosition.IsEnabled = false;
                        //playingPanel.Visibility = Visibility.Collapsed;
                        curPosition.Value = 0;
                        break;

                    case EasyPlayerState.Playing:
                        playStop.Source =
                            new BitmapImage(new Uri("../../Resources/player/play_Pause.png", UriKind.RelativeOrAbsolute));
                        curPosition.IsEnabled = true;
                        UpdateText();
                        failImage.Visibility = Visibility.Collapsed;
                        ShowPlaying();
                        break;

                    case EasyPlayerState.Paused:
                        curPosition.IsEnabled = true;
                        playStop.Source =
                            new BitmapImage(new Uri("../../Resources/player/play_Play.png", UriKind.RelativeOrAbsolute));
                        HidePlaying();
                        break;
                }
                         
                _state = value;

            }
        }

        

        public void Play(Uri file)
        {
            if (file != null)
            {
                if (mainPlayer.Source == null || mainPlayer.Source.AbsoluteUri==null || file.AbsoluteUri.ToUpper() != mainPlayer.Source.AbsoluteUri.ToUpper())
                            {
                                ShowPlaying();
                                mainPlayer.Source = file;
                            }
                else
                {
                    if(mainPlayer.CurrentState==MediaElementState.Paused)
                    {
                        mainPlayer.Play();
                        State = EasyPlayerState.Playing;
                        ShowPlaying();
                    }

                }
            }
        }

        private void media_Ended(object sender, RoutedEventArgs e)
        {
            State = EasyPlayerState.Paused;
        }

        private void media_Failed(object sender, ExceptionRoutedEventArgs e)
        {
            State = EasyPlayerState.NoMedia;
            failImage.Visibility = Visibility.Visible;
        }

        private void ShowPlaying()
        {
            /*playingPanel.Visibility = Visibility.Visible;
            PlayPlaying.Begin();*/
            //todo: в заголовок окна
        }
        private void HidePlaying()
        {
            /*playingPanel.Visibility = Visibility.Collapsed;
            PlayPlaying.Stop();*/
            //todo: в заголовок окна
        }

        private void playStop_Clicked(object sender, MouseButtonEventArgs e)
        {
            switch (_state)
            {
                case EasyPlayerState.Playing:
                     mainPlayer.Pause();
                     State = EasyPlayerState.Paused;
                    break;
                case EasyPlayerState.Paused:
                    mainPlayer.Play();
                    State = EasyPlayerState.Playing;
                    break;
                default:
                case EasyPlayerState.NoMedia:
                    break;
            }
        }

        private void opened(object sender, RoutedEventArgs e)
        {
            mainPlayer.Play();
            failImage.Visibility = Visibility.Collapsed;
            State = EasyPlayerState.Playing;
        }

        private void pos_Seeked(object sender, GenericEventArgs<double> e)
        {
            if (mainPlayer.CurrentState == MediaElementState.Paused || mainPlayer.CurrentState == MediaElementState.Playing)
            {
                mainPlayer.Position =
                    TimeSpan.FromSeconds(e.Parameter * mainPlayer.NaturalDuration.TimeSpan.TotalSeconds);
            }
        }
    }
}
